import 'product.dart';

class Game {
  final String id;
  final String name;
  final String description;
  final String coverImage;
  final String developer;

  //这个字段不会存储在games表中
  //它是一个在应用逻辑中使用的辅助字段，由Repository填充。
  final List<Product> products;

  Game({
    required this.id,
    required this.name,
    required this.description,
    required this.coverImage,
    required this.developer,
    this.products = const [], //默认为空列表
  });

  //不填充products字段，因为它不属于games表的列。由copyWith方法对该字段赋值
  factory Game.fromMap(Map<String, dynamic> map) {
    return Game(
      id: map['id'] as String,
      name: map['name'] as String,
      description: map['description'] as String,
      coverImage: map['coverImage'] as String,
      developer: map['developer'] as String,
    );
  }

  Map<String, dynamic> toMap() {
    //toMap方法不包含products列表，因为它不属于games表的列
    return {
      'id': id,
      'name': name,
      'description': description,
      'coverImage': coverImage,
      'developer': developer,
    };
  }

  //提供一个copyWith方法，方便在获取到产品列表后更新Game对象
  Game copyWith({List<Product>? products}) {
    return Game(
      id: id,
      name: name,
      description: description,
      coverImage: coverImage,
      developer: developer,
      products: products ?? this.products,
    );
  }
}